package com.leet.code.thread;

import java.util.function.IntConsumer;

public class Code1195 {
    public static void main(String[] args) {

    }

    static class FizzBuzz {
        private int n;
        private volatile int i = 1;

        public FizzBuzz(int n) {
            this.n = n;
        }

        // printFizz.run() outputs "fizz".
        public void fizz(Runnable printFizz) throws InterruptedException {

            while (i <= n) {
                if (i % 3 == 0 && i % 5 != 0) {
                    printFizz.run();
                    i++;
                } else {
                    Thread.yield();
                }
            }

        }

        // printBuzz.run() outputs "buzz".
        public void buzz(Runnable printBuzz) throws InterruptedException {
            while (i <= n) {
                if (i % 5 == 0 && i % 3 != 0) {
                    printBuzz.run();
                    i++;
                } else {
                    Thread.yield();
                }
            }
        }

        // printFizzBuzz.run() outputs "fizzbuzz".
        public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
            while (i <= n) {
                if (i % 5 == 0 && i % 3 == 0) {
                    printFizzBuzz.run();
                    i++;
                } else {
                    Thread.yield();
                }
            }
        }

        // printNumber.accept(x) outputs "x", where x is an integer.
        public void number(IntConsumer printNumber) throws InterruptedException {
            while (i <= n) {
                if (i % 5 != 0 && i % 3 != 0) {
                    printNumber.accept(i);
                    i++;
                } else {
                    Thread.yield();
                }
            }
        }
    }
}
